<template>
  <div>
    <div style="margin: 10px 0;">
      <el-button type="danger" icon="el-icon-delete" @click="delBatch">批量删除</el-button>
      <el-input placeholder="请输入名称查询" v-model="name" clearable style="width: 200px;margin-left: 5px"></el-input>

      <el-select v-model="localName" placeholder="请选择学校" @change="select" style="margin-left: 10px">
        <el-option
            v-for="item in localNames" :key="item.localeName" :label="item.localeName" :value="item.localeName">
        </el-option>
      </el-select>
      <el-button type="primary" icon="el-icon-search" style="margin-left: 5px" @click="search">查询</el-button>
      <el-button type="warning" icon="el-icon-refresh" style="margin-left: 5px" @click="reset">重置</el-button>
    </div>
    <el-table :data="tableData" @selection-change="handleSelectionChange" style="width: 100%"
              :header-cell-style="{background:'#e7e8ef',color:'#4f5257'}"
              stripe fixed border>
      <el-table-column
          prop="id"
          type="selection"
          width="40">
      </el-table-column>
      <el-table-column prop="localName" label="学校名" ></el-table-column>
      <el-table-column  label="姓名" >
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <el-tag size="medium">{{ scope.row.nickName }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="telphone" label="电话号码" width="120"></el-table-column>
      <el-table-column prop="idCard" label="身份证号"></el-table-column>
      <el-table-column prop="temperature" label="体温" ></el-table-column>
      <el-table-column prop="heathCode" label="健康码颜色" ></el-table-column>
      <el-table-column prop="heathImg" label="健康码图片" width="130">
        <template slot-scope="scope">
          <el-image style="width: 100px; height: 100px" :src="scope.row.heathImg" :preview-src-list="[scope.row.heathImg]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="remarks" label="目的" ></el-table-column>
      <el-table-column prop="time" label="时间" width="150"></el-table-column>
      <el-table-column prop="state" label="进/出" width="50"></el-table-column>
      <el-table-column prop="process" label="审核状态" ></el-table-column>
      <el-table-column label="审核" v-if="user.role === 'ROLE_ADMIN'" width="240">
        <template v-slot="scope">
          <el-button type="success" @click="changeState(scope.row, '审核通过')" :disabled="scope.row.process !== '待审核'">审核通过</el-button>
          <el-button type="danger" @click="changeState(scope.row, '审核不通过')" :disabled="scope.row.process !== '待审核'">审核不通过</el-button>
        </template>
      </el-table-column>
      <el-table-column
          label="操作"
          width="100">
        <template slot-scope="scope">
          <el-popconfirm
              style="margin-left: 5px"
              confirm-button-text='确定'
              cancel-button-text='取消'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="handleDelete(scope.$index, scope.row)">
            <el-button type="danger" size="mini" class="el-icon-remove-outline" slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 30]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>

import {serverIp} from "../../../public/config";

export default {
  name: "Carts",
  data(){
    return{
      serverIp: serverIp,
      name: "",
      tableData:[],
      currentPage: 4,
      pageNum:0,
      pageSize:5,
      total:0,
      dialogVisible: false,
      form: {},
      ids:[],
      multipleSelection: [],
      roles: [],
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      localNames:[],
      localName:""
    }
  },
  created() {
    this.load()
  },
  methods:{
    reset(){
      this.name="";
      this.localName=""
      this.load()
      this.$message({
        message: '重置成功',
        type: 'success'
      });
    },
    search(){
      if (this.name!=''||this.name!=null){
        this.load()
        this.$message({
          message: '查询成功',
          type: 'success'
        });
      }else{
        this.$message({
          message: '请输入查询内容',
          type: 'warning'
        });
      }
    },
    changeState(row, process) {
      this.form = JSON.parse(JSON.stringify(row))
      this.form.process = process;
      this.save();
    },
    select(){
      this.load()
    },
    save() {
      this.request.post("/t-access", this.form).then(res => {
        if (res.code === 200) {
          this.$message.success("保存成功")
          this.load()
        } else {
          this.$message.error("保存失败")
        }
      })
    },
    load(){
      this.request.get("/t-access/page/school",{
            params:{
              pageNum:this.pageNum,
              pageSize:this.pageSize,
              name:this.name,
              localName:this.localName,
            }
          }
      ).then(res =>{
        this.tableData=res.data.records
        this.total=res.data.total
      })

      //获取系统的学校地点名
      this.request.get("/localName/byLocale/"+"学校").then(res =>{
        this.localNames = res.data
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
    },
    handleDelete(index, row) {
      this.request.delete("/t-access/"+(index,row).id).then(res=>{
        if (res.code===200){
          this.load()
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }
      })
    },
    delBatch(){
      let ids = this.multipleSelection.map(v =>v.id)
      if (ids.length!=0){
        this.$confirm('你正在进行批量删除操作, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.request.post("/t-access/del/batch/", ids).then(res => {
            if (res.code===200){
              this.load()
              this.$message({
                type: 'success',
                message: '批量删除成功!'
              });
            }else {
              this.$message({
                type: 'error',
                message: res.message
              });
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }else {
        this.$message.warning("请选择删除记录")
      }
    },
    /**
     * 选择框
     * */
    handleSelectionChange(val) {
      this.multipleSelection = val;
      for (let i=1; i<=val; i++)
        console.log(this.multipleSelection[i])
    },
    /**
     * 自定义操作列
     * @param index
     * @param row
     */
    handleSizeChange(val) {
      this.pageSize = val;
      this.load()
    },
    handleCurrentChange(val) {
      this.pageNum=val
      this.load()
    },
    handleAvatarSuccess(res){
      this.form.img=res
    },
  }
}
</script>

<style scoped>

</style>